package com.ruoyi.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DetectionTask;
import com.ruoyi.system.service.IDetectionTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检测任务Controller
 * 
 * @author ruoyi
 * @date 2025-10-22
 */
@Controller
@RequestMapping("/system/task")
public class DetectionTaskController extends BaseController
{
    private String prefix = "system/task";

    @Autowired
    private IDetectionTaskService detectionTaskService;

    @RequiresPermissions("system:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询检测任务列表
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DetectionTask detectionTask)
    {
        startPage();
        List<DetectionTask> list = detectionTaskService.selectDetectionTaskList(detectionTask);
        return getDataTable(list);
    }

    /**
     * 导出检测任务列表
     */
    @RequiresPermissions("system:task:export")
    @Log(title = "检测任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DetectionTask detectionTask)
    {
        List<DetectionTask> list = detectionTaskService.selectDetectionTaskList(detectionTask);
        ExcelUtil<DetectionTask> util = new ExcelUtil<DetectionTask>(DetectionTask.class);
        return util.exportExcel(list, "检测任务数据");
    }

    /**
     * 新增检测任务
     */
    @RequiresPermissions("system:task:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存检测任务
     */
    @RequiresPermissions("system:task:add")
    @Log(title = "检测任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DetectionTask detectionTask)
    {
        return toAjax(detectionTaskService.insertDetectionTask(detectionTask));
    }

    /**
     * 修改检测任务
     */
    @RequiresPermissions("system:task:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        DetectionTask detectionTask = detectionTaskService.selectDetectionTaskByTaskId(taskId);
        mmap.put("detectionTask", detectionTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存检测任务
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "检测任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DetectionTask detectionTask)
    {
        return toAjax(detectionTaskService.updateDetectionTask(detectionTask));
    }

    /**
     * 删除检测任务
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "检测任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(detectionTaskService.deleteDetectionTaskByTaskIds(ids));
    }
}
